package com.cheng.xbjy.scheduled;

import com.cheng.xbjy.entity.Meeting;
import com.cheng.xbjy.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class MeetingScheduled {

    @Autowired
    private MeetingService meetingService;

    //每秒执行一次
    @Scheduled(cron = "1 * * * * ?")
    public void meetingStatus() {
        System.out.println("定时器执行了");
        List<Meeting> meetingList = meetingService.findByStatusNot(2L);

        if (meetingList.size() > 0) {
            for (Meeting meeting : meetingList) {
                Date date = new Date();
                Date startTime = meeting.getStartTime();
                Date endTime = meeting.getEndTime();
                //说明会议要开始了
                if (date.getTime() > startTime.getTime() && meeting.getStatus() == 0L) {
                    meetingService.updateStatus(meeting.getId(), 1L);
                }

                //说明会议要结束了
                if (date.getTime() > endTime.getTime() && meeting.getStatus() == 1L) {
                    meetingService.updateStatus(meeting.getId(), 2L);
                }
            }
        }
    }


}
